<!DOCTYPE html>
<html lang="zh">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>购物车布局</title>
  <link href="css/cart.css" rel="stylesheet">
</head>

<body>

  <div id="app" class="container">

    <!-- 空购物车界面 -->
    <div class="empty-cart-message" v-if="cartItems.length === 0">
      <div class="empty-cart-image"></div>
      您的购物车空空如也
      <a href="#" class="go-checkout">去购物</a>
    </div>

    <div class="cart" v-else>
      <h1>科泰淘购物车</h1>
      <div class="clearfix">
        <a href="#" class="delete-selected" @click="deleteSelectedItems">删除选中</a>
        <a href="#" class="clear-cart" @click="clearCart">清空购物车</a>
      </div>
      <div class="cart-item" v-for="(item, index) in cartItems" :key="index">
        <div class="cart-item-details">
          <input type="checkbox" class="cart-item-checkbox" v-model="item.selected">
          <img :src="item.image" :alt="item.name">
          <div class="cart-item-name-sku">
            <div class="cart-item-name">{{ item.name }}</div>
            <div class="cart-item-sku">颜色：{{ item.color }} 尺码:{{ item.size }}</div>
          </div>
        </div>
        <div class="cart-item-quantity-price">
          <div class="cart-item-quantity">
            <button class="quantity-btn minus-btn" @click="decrementQuantity(index)">-</button>
            <input type="text" name="quantity" class="quantity" v-model.number="item.quantity">
            <button class="quantity-btn plus-btn" @click="incrementQuantity(index)">+</button>
          </div>
          <div class="cart-item-price">￥{{ item.price }}</div>
        </div>
      </div>

      <div class="total-price-container">
        <div class="total-count">已选 <span class="count-value">{{ selectedCount }}</span> 件</div>
        <div class="total-price">总计:￥{{ totalPrice }}</div>
      </div>

      <div class="clearfix">
        <div class="select-all-container">
          <input type="checkbox" id="select-all-checkbox" class="cart-item-checkbox" v-model="selectAll">
          <label for="select-all-checkbox">全选</label>
        </div>
        <a href="#" class="go-checkout">去结算</a>
      </div>
    </div>

  </div>

  <script src="./js/vue.js"></script>
  <script>
    new Vue({
      el: '#app',
      data: {
        cartItems: [
          { name: '男士休闲裤夏季新款', color: '灰色', size: 'L', price: 99, quantity: 1, selected: false, image: 'images/goods-1.png' },
          { name: '男士夏季装短袖polo衫', color: '浅蓝', size: 'M', price: 199, quantity: 1, selected: false, image: 'images/goods-2.png' }
        ]
      },
      computed: {
        selectedCount() {
          return this.cartItems.filter(item => item.selected).length;
        },
        totalPrice() {
          return this.cartItems.reduce((total, item) => {
            if (item.selected) {
              return total + item.price * item.quantity;
            }
            return total;
          }, 0);
        },
        selectAll: {
          get() {
            return this.cartItems.every(item => item.selected);
          }, 
          set(value) {
            this.cartItems.forEach(item => {
              item.selected = value;
            });
          }
        }
      },
      methods: {
        incrementQuantity(index) {
          this.cartItems[index].quantity++;
        },
        decrementQuantity(index) {
          if (this.cartItems[index].quantity > 1) {
            this.cartItems[index].quantity--;
          }
        },
        deleteSelectedItems() {
          this.cartItems = this.cartItems.filter(item => !item.selected);
        },
        clearCart() {
          this.cartItems = [];
        }
      }
    });
  </script>
</body>

</html>